package com.sevenit.nj.yrmtfish.common;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.sevenit.nj.yrmtfish.entity.ServerData;

/**  
*  @description: 工具类 .
*  @version 1.0  
*  @author wangxw
*/  
public class Utils {   

	/**
	 * isNull
	 * @param strIn
	 * @return
	 */
	public static boolean isNull(String strIn) {
		if (strIn == null || strIn.length() == 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除前后全半角空格.
	 * 
	 * @param strIn 输入的将要被删除前后空格的字符串.
	 * @param bFullSpaceDel 判断前后的全角空格是否被删除.
	 * @return 删除前后空格以后的字符串.
	 */
	public static String delSpace(String strIn, boolean bFullSpaceDel) {
		if (isNull(strIn)) {
			return strIn;
		}

		boolean bFlag = true;
		strIn = strIn.trim();

		while (bFlag && bFullSpaceDel) {
			if (strIn.length() <= 0) {
				break;
			}
			String strStart = strIn.substring(0, 1);
			String strEnd = strIn.substring(strIn.length() - 1);

			if (strEnd.equalsIgnoreCase("　") || strEnd.equalsIgnoreCase(" ")) {
				strIn = strIn.substring(0, strIn.length() - 1);
			} else if (strStart.equalsIgnoreCase("　") || strStart.equalsIgnoreCase(" ")) {
				strIn = strIn.substring(1, strIn.length());
			} else {
				bFlag = false;
			}
		}
		return strIn;
	}
	
	/**
	 * 判断是否为半角数字整数,空返回false
	 * 
	 * @param strIn
	 *            被判断的字符串.
	 * @return true 全部字符为半角数字(整数).
	 */
	public static boolean isHalfNum(String strIn) {
		if (isNull(strIn)) {
			return false;
		}

		char c;
		for (int i = 0; i < strIn.length(); i++) {
			c = strIn.charAt(i);
			if ((c <= 0x0039 && c >= 0x0030) == false)
				return false;
		}
		return true;
	}
	
	/**
	 * 判断是否为半角的小数,空返回false
	 * 
	 * @param strIn
	 *            被判断的字符串
	 * @return true 全部字符为半角数字
	 */
	public static boolean isHalfDec(String strIn) {
		if (isNull(strIn)) {
			return false;
		}

		if (strIn.indexOf(".") != strIn.lastIndexOf(".")) {
			return false;
		}

		char c;
		for (int i = 0; i < strIn.length(); i++) {
			c = strIn.charAt(i);
			if (((c <= 0x0039 && c >= 0x0030) || c == '.') == false)
				return false;
		}
		return true;
	}
	
	/**
	 * 取得一个字符串的字节数.
	 * 
	 * @param s
	 *            输入的字符串.
	 * @param encoding
	 *            字符串的编码（如：GB2312）,可以为null
	 * @return 字符串的字节数.
	 */
	public static int getStringByteLength(String s, String encoding) {
		if (isNull(s)) {
			return 0;
		}

		int len = 0;
		for (int i = 0; i < s.length(); i++) {
			try {
				byte[] b = null;
				if (!isNull(encoding)) {
					b = s.substring(i, i + 1).getBytes(encoding);
				} else {
					b = s.substring(i, i + 1).getBytes();
				}

				len = len + b.length;
			} catch (Exception e) {
				continue;
			}

		}
		return len;
	}
	
	/***
	 * 取得一个字符串的前面最大length字节长度的部分.
	 * @param s
	 * @param encoding
	 * @param length
	 * @return
	 */
	public static String subStrByteLen(String s, String encoding, int length) {
		
		String strRe = "";
		
		if (isNull(s)) {
			return s;
		}
		if (length <= 0){
			return strRe;
		}

		int len = 0;
		String strT;
		for (int i = 0; i < s.length(); i++) {
			try {
				byte[] b = null;
				strT = s.substring(i, i + 1);
				if (!isNull(encoding)) {
					b = strT.getBytes(encoding);
				} else {
					b = strT.getBytes();
				}

				len = len + b.length;
			} catch (Exception e) {
				continue;
			}
			if(len > length){
				break;
			}
			strRe += strT;

		}
		return strRe;
	}
	
	/**
	 * 从HTTP地址中提取文件名.
	 * @param strHttp
	 * @return
	 */
	public static String getFileNameFromHttp(String strHttp) {
		
		String strFileName = "";
		
		//HTTP地址为空.
		if(isNull(strHttp)){
			return strFileName;
		}
		
		//截取文件名.
		strFileName = strHttp;
		if(strHttp.indexOf("/") >= 0){
			strFileName = strHttp.substring(strHttp.lastIndexOf("/") + 1);
		}
		
		return strFileName;
	}
	
	/**
	 * HTTP GET请求.
	 * @param strUrL
	 * @return
	 */
	public static String HttpGet(String strUrL) {
		
		String result = null;
		
		if(isNull(strUrL)){
			return result;
		}
		
		try {
			HttpGet httpGet = new HttpGet(strUrL);
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
			
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(httpResponse.getEntity());
			}
		}catch (Exception e) {
		}
		
		return result;
	}
	
	/***
	 * 取得一个字符串的前面最大length长度的部分.
	 * @param s
	 * @param length
	 * @return
	 */
	public static String subLen(String s, int length) {
		
		if (isNull(s) || s.length() <= length) {
			return s;
		}
		if (length <= 0){
			return "";
		}

		return s.substring(0,length);
	}
	
	/**
	 * 创建ID+图片对字串.
	 * @return
	 */
	public static String addIdImgs(String strId, String strImg, String strName) {
		
		String strNameRe = strName;
		
		if(Utils.isNull(strNameRe)){
			strNameRe = "";
		}else{
			strNameRe = strNameRe.replaceAll(",", "，")	;
			strNameRe = strNameRe.replaceAll("\\|", "｜")	;
		}
		
		return (strId + "," + strImg + "," + strNameRe + "|");
	}
	
	/**
	 * 获取ID+图片对.
	 * @return
	 */
	public static ServerData getIdImgs(String strIdImgs) {
		ServerData sd = new ServerData();
		
		if(strIdImgs.length() > 0){
			String[] strItem = strIdImgs.split("\\|");
			for(int i = 0;i <strItem.length;i++){
				String[] strVal = strItem[i].split(",");
				if(strVal.length < 3 || isNull(strVal[0])){
					continue;
				}
				sd.newItem();
				sd.addString("Id",  strVal[0]);
				sd.addString("PicAdd",  strVal[1]);
				sd.addString("Name",  strVal[2]);
				sd.closeItem();
			}
		}
		
		return sd;
	}
	
	/**
	 *  判断一个用逗号分隔的字串中是否包含传入的字串.
	 * @param strIds : 用逗号分隔的字串.
	 * @param strId : 传入的字串.
	 * @return
	 */
	public static boolean strExist(String strIds, String strId) {
		
		if(isNull(strIds) || isNull(strId)){
			return false;
		}
		
		String[] ids = strIds.split(",");
		for(int i = 0; i < ids.length;i++){
			if(strId.equals(ids[i])){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 *  从字串中删除给定的值.
	 * @param strIds : 用逗号分隔的字串.
	 * @param strId : 传入的字串.
	 * @return 删除后的字串.
	 */
	public static String strDelFrom(String strIds, String strId) {
		
		String strIdsRe = "";
		
		if(isNull(strIds) || isNull(strId)){
			return strIds;
		}
		
		String[] ids = strIds.split(",");
		for(int i = 0; i < ids.length;i++){
			if(!strId.equals(ids[i])){
				strIdsRe += ids[i] + ",";
			}
		}
		
		if(!isNull(strIdsRe)){
			strIdsRe = strIdsRe.substring(0,strIdsRe.length() - 1);
		}
		
		return strIdsRe;
	}
	
	/**
	 *  向字串中添加给定的值.
	 * @param strIds : 用逗号分隔的字串.
	 * @param strId : 传入的字串.
	 * @param bCheckExist : true的时候，如果已经存在不添加.
	 * @return 添加后的字串.
	 */
	public static String strAddTo(String strIds, String strId, boolean bCheckExist) {
		
		if(isNull(strId)){
			return strIds;
		}
		
		if(isNull(strIds)){
			return strId;
		}
		
		//判断是否存在
		if(bCheckExist){
			String[] ids = strIds.split(",");
			for(int i = 0; i < ids.length;i++){
				if(strId.equals(ids[i])){
					return strIds;
				}
			}
		}
		
		return (strIds + "," + strId);
	}
	/**
	 * AMap对象判断是否为null
	 * 
	 * @param context
	 * @param aMap
	 * @return
	 */
	public static boolean checkReady(Context context, AMap aMap) {
		if (aMap == null) {
			Toast.makeText(context, "map is loading", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
}  
